
#include <string>
#include <memory>
#include <mutex>
using namespace std;

#include <time.h>

#include "Timestamp.hpp"
#include "AppendFile.hpp"

#ifndef LOGFILE_HPP
#define LOGFILE_HPP

namespace tulun
{
    class LogFile
    {
    private:
        const std::string basename_; // qwer
        const size_t rollSize_;      // 10M
        const int flushInterval_;    // 3s
        const int checkEventN_;
        int count_;

    private:
        time_t startOfPeriod_;
        time_t lastRoll_;
        time_t lastFlush_;
        static const int kRollPerSeconds_ = 60 * 60 * 24;

    private:
        std::unique_ptr<tulun::AppendFile> file_;
        std::unique_ptr<std::mutex> mutex_;

    private:
        static std::string getLogFileName(const std::string &basename, const tulun::Timestamp &now);
        void append_unlocked(const char *msg, const size_t len);

    public:
        LogFile(const std::string &basename,
                size_t rollSize = 1024 * 128,
                int flushInterval = 3,
                int checkEventN = 30,
                bool threadSafe = true);
        ~LogFile();
        void append(const std::string &msg);
        void append(const char *msg, const size_t len);
        void flush();
        bool rollFile();
    };
} // namespace tulun

#endif